package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class NanduNewsAnalyse implements AnalyseNews{

	private static final String[] regex={
				"http://.*.nandu.com/nis/[0-9]+/[0-9]+/[0-9]+.html",
				"http://.*.nandu.com/html/[0-9]+/[0-9]+/[0-9]+.html",
				"http://www.oeeee.com/html/[0-9]{8}/[0-9]{2}/[0-9]+.html"

	};
		
		public boolean isDetailPage(String url) {
			for (int i=0; i < regex.length; i++) {
				if (url.matches(regex[i])) {
					return true;
				}
			}
			return false;
		}

		
		public NewsMeta Update(NewsMeta meta) {
			return null;
		}

		
		public NewsMeta parserHtml(UrlMeta urlMeta) {
			NewsMeta news=new NewsMeta();
			if (urlMeta.getHtml() == null) {
			}
			String htmltxt=urlMeta.getHtml();
			String url=urlMeta.getUrl();
			if(!isDetailPage(url)){
			}


			String title=null;
			String content=null;
			Long date=null;
			Document doc=Jsoup.parse(htmltxt);

			doc.select(".side").remove();

			title=doc.select("div.title h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content p"));
			content = content.replaceAll("<[a-z\\/\\ ]+>", "");
			date=DateUtils.matchDate(doc.select("span.time").text());
			String author=JSoupUtils.matchAuthor(doc, "来源:");
			news.setAuthor(author);
			news.setUrl(url);
			news.setTitle(title);
			news.setContent(content);
			news.setDate(date);
			return news;
		}

		public static void main(String[] args) {
			NanduNewsAnalyse test=new NanduNewsAnalyse();

			String url="http://www.oeeee.com/html/201605/11/393485.html";
			UrlMeta meta=CrawlHTML.responseToURL(url);
			NewsMeta parserHtml=test.parserHtml(meta);
			System.out.println(parserHtml);
		}
	
	public boolean isNeedUpdate(){
		return false;
	}

}
